package com.cn.crmsystem.controller.systemController;


import com.cn.crmsystem.exception.TokenExpiredException;
import com.cn.crmsystem.pojo.ResultJson;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
public class ExceptionController {
    @RequestMapping("/unauthorizedUrl")
    public ResultJson getUnauthorizedUrl() {
        return new ResultJson().getFail("未授权,无权访问");
    }

    @RequestMapping("/notLogin")
    public ResultJson notLogin(HttpServletRequest request) {//  未登录
        System.out.println("--------------------");
        return new ResultJson().getFail((String) request.getAttribute("msg"));
    }


    // 捕捉shiro的异常
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(ShiroException.class)
    public ResultJson handle401(ShiroException e) {
        return new ResultJson().getFail(e.getMessage());
    }

    // 捕捉未登录的异常
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UnauthenticatedException.class)
    public ResultJson handle401(UnauthenticatedException e) {
        return new ResultJson().getFail("你还没有登录");
        // return ResultJson.fail(401, , null);
    }

    // 捕捉没有相应的权限或者角色的异常
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    @ExceptionHandler(UnauthorizedException.class)
    public ResultJson handle401(UnauthorizedException e) {
        System.out.println(e.getMessage());
        return new ResultJson().getFail("你没有权限访问" + e.getMessage());
    }


    /**
     * @Validated 校验错误异常处理
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResultJson handler(MethodArgumentNotValidException e) throws IOException {
//        log.error("运行时异常:-------------->",e);
        BindingResult bindingResult = e.getBindingResult();
        //这一步是把异常的信息最简化
        ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();
        return new ResultJson().getFail(objectError.getDefaultMessage());
    }

    /**
     * 处理Assert的异常
     */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public ResultJson handler(IllegalArgumentException e) throws IOException {
//        log.error("Assert异常:-------------->{}",e.getMessage());
        return new ResultJson().getFail(e.getMessage());
    }


    //运行时错误处理
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = RuntimeException.class)
    public ResultJson handle(RuntimeException e) {
        return new ResultJson().getFail(e.getMessage());
    }


    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = TokenExpiredException.class)
    public ResultJson handler(TokenExpiredException e) throws IOException {
        return new ResultJson().getFail("token已经过期，请重新登录");
    }

}
